/**
 * @param {number[]} nums
 * @return {number}
 */
var wiggleMaxLength = function (nums) {
    let len = nums.length;
    if (len === 1) return 1;
    let dp = new Array(len).fill(0),
    preDiff = nums[0] - nums[1];
    dp[0] = 1
    dp[1] = preDiff === 0 ? 1 : 2

    for (let i = 2; i < nums.length; i++) {
        let curDiff = nums[i - 1] - nums[i];
        if ((preDiff === 0 && curDiff !== 0) || (preDiff * curDiff < 0)) {
            dp[i] = dp[i - 1] + 1
            preDiff = curDiff
        } else {
            dp[i] = dp[i - 1]
        }
    }

    return dp[len - 1]
};